package com.epam.player.desktop.view;

import java.awt.Color;
import java.awt.Font;
import java.util.Map;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.epam.player.desktop.common.Constant;
import com.epam.player.desktop.exceptions.PropertiesLoadException;
import com.epam.player.desktop.logic.ActionClickEventLogin;
import com.epam.player.desktop.logic.KeyAdapterLogin;
import com.epam.player.desktop.logic.PropertiesLoader;

public class LoginForm {
	private JTextField textField;
	private JPasswordField passwordField;
	private JLabel lblMessage;
	private JFrame loginForm;
	private JLabel lblNewLabel;
	private JLabel lblPassword;
	private JButton buttonLogin;

	/**
	 * @wbp.parser.entryPoint
	 */

	public void showFormLogin() {
		loginForm = new JFrame("Login");
		lblNewLabel = new JLabel("Email");
		lblPassword = new JLabel("Password");
		textField = new JTextField();
		passwordField = new JPasswordField();
		lblMessage = new JLabel();
		buttonLogin = new JButton("Login");

		loginForm.setLocation(300, 200);
		loginForm.setResizable(false);
		lblMessage.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMessage.setForeground(new Color(255, 69, 0));
		loginForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		textField.setColumns(10);

		PropertiesLoader loader = new PropertiesLoader();
		try {
			Map<String, String> data = loader.getProperties();
			textField.setText(data.get(Constant.EMAIL));
			passwordField.setText(data.get(Constant.PASS));
		} catch (PropertiesLoadException e) {
			textField.setText("");
		}

		KeyAdapterLogin adapterLogin = new KeyAdapterLogin(buttonLogin);
		textField.addKeyListener(adapterLogin);
		passwordField.addKeyListener(adapterLogin);

		ActionClickEventLogin actionClickEventLogin = new ActionClickEventLogin(
				buttonLogin, lblMessage, loginForm, textField, passwordField);

		buttonLogin.addActionListener(actionClickEventLogin);

		initGroupLayout();
		loginForm.pack();
		loginForm.setVisible(true);

	}

	private void initGroupLayout() {
		GroupLayout groupLayout = new GroupLayout(loginForm.getContentPane());
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								Alignment.LEADING,
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblMessage,
																GroupLayout.PREFERRED_SIZE,
																203,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								lblPassword)
																						.addComponent(
																								lblNewLabel))
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								buttonLogin)
																						.addGroup(
																								groupLayout
																										.createParallelGroup(
																												Alignment.LEADING,
																												false)
																										.addComponent(
																												passwordField)
																										.addComponent(
																												textField)))))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap(17, Short.MAX_VALUE)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGap(22)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.BASELINE)
																						.addComponent(
																								passwordField,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								lblPassword)))
														.addGroup(
																groupLayout
																		.createParallelGroup(
																				Alignment.BASELINE)
																		.addComponent(
																				textField,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				lblNewLabel)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(buttonLogin)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(lblMessage).addGap(28)));
		initGroupLayout(loginForm, buttonLogin, lblNewLabel, lblPassword,
				groupLayout);
	}

	private void initGroupLayout(final JFrame loginForm, JButton buttonLogin,
			JLabel lblNewLabel, JLabel lblPassword, GroupLayout groupLayout) {
		loginForm.getContentPane().setLayout(groupLayout);
	}
}
